package com.shopx.common.service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import com.shopx.auth.dao.db1.SysOrgDao;
import com.shopx.auth.model.SysOrgModel;
import com.shopx.core.common.CommonConstant;
import com.shopx.dal.redis.RedisCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 钩子程序，启动载入缓存
 */
@Component
@Slf4j
public class StartupInitializer {

    @Resource
    private RedisCacheService redisCacheService;

    @Resource
    private SysOrgDao sysOrgDao;


    @PostConstruct
    public void init() {
        log.info("钩子程序启动");
        this.loadOrg();
        log.info("钩子程序结束");

    }

    /**
     * 载入组织机构
     */
    private void loadOrg() {
        SysOrgModel param = new SysOrgModel();
        List<SysOrgModel> orgList = sysOrgDao.selectList(param);
        for(SysOrgModel record:orgList){
            if(record==null){
                continue;
            }
            log.info("将orgId:{}写入缓存",record.getOrgId());
            redisCacheService.set(CommonConstant.CacheKey.ORG_CACHE+record.getOrgId(),record);
        }
        //写入整个组织机构list
        redisCacheService.set(CommonConstant.CacheKey.ORG_LIST_CACHE,orgList);
    }

}
